<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流媒体播放终端</title>
    <link rel="stylesheet" href="../css/all.min.css">
    <script src="../js/hls.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background: #fff;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1100px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        
        .tab-content {
            padding: 30px;
        }
        
        .url-input {
            width: 100%;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
            margin-bottom: 15px;
        }
        
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: background-color 0.3s;
            margin: 5px;
        }
        
        .btn:hover {
            background-color: #2980b9;
        }
        
        .btn-danger {
            background-color: #e74c3c;
        }
        
        .btn-danger:hover {
            background-color: #c0392b;
        }
        
        .video-container {
            width: 100%;
            margin: 20px 0;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        video {
            width: 100%;
            height: 500px;
            display: block;
            background: #000;
        }
        
        .status {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }
        
        .status.info {
            background-color: #d6eaf8;
            color: #2874a6;
        }
        
        .status.success {
            background-color: #d5f5e3;
            color: #1d8348;
        }
        
        .status.error {
            background-color: #fadbd8;
            color: #c0392b;
        }
        
        .note {
            margin-top: 25px;
            padding: 15px;
            background-color: #fff8e1;
            border-left: 4px solid #ffc107;
            font-size: 14px;
        }
        
        @media (max-width: 768px) {
            .tab-content {
                padding: 20px;
            }
            
            .btn {
                width: 100%;
                margin: 5px 0;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="tab-content">
            <input type="text" class="url-input" id="m3u8Url" placeholder="请输入流媒体地址">
            <button class="btn" id="playBtn"><i class="fas fa-play"></i> 播放</button>
            <button class="btn btn-danger" id="stopBtn"><i class="fas fa-stop"></i> 停止</button>
            
            <div class="video-container">
                <video id="videoPlayer" controls></video>
            </div>
            
            <div class="status info" id="playerStatus"></div>
            
            <div class="note">
                <p><strong>提示：</strong>支持HLS协议流媒体播放。若无法播放，可能是由于CORS限制或视频源问题。</p>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const m3u8UrlInput = document.getElementById('m3u8Url');
            const playBtn = document.getElementById('playBtn');
            const stopBtn = document.getElementById('stopBtn');
            const videoPlayer = document.getElementById('videoPlayer');
            const playerStatus = document.getElementById('playerStatus');
            let hlsInstance = null;
            
            playBtn.addEventListener('click', function() {
                const m3u8Url = m3u8UrlInput.value.trim();
                if (!m3u8Url) {
                    showPlayerStatus('请输入有效的流媒体地址', 'error');
                    return;
                }
                
                if (hlsInstance) {
                    hlsInstance.destroy();
                    hlsInstance = null;
                }
                
                if (Hls.isSupported()) {
                    initHlsPlayer(m3u8Url);
                } else if (videoPlayer.canPlayType('application/vnd.apple.mpegurl')) {
                    initNativeHlsPlayer(m3u8Url);
                } else {
                    showPlayerStatus('您的浏览器不支持流媒体播放', 'error');
                }
            });
            
            stopBtn.addEventListener('click', function() {
                videoPlayer.pause();
                videoPlayer.src = '';
                
                if (hlsInstance) {
                    hlsInstance.destroy();
                    hlsInstance = null;
                }
                
                showPlayerStatus('播放已停止', 'info');
            });
            
            function initHlsPlayer(url) {
                hlsInstance = new Hls();
                
                hlsInstance.loadSource(url);
                hlsInstance.attachMedia(videoPlayer);
                
                hlsInstance.on(Hls.Events.MANIFEST_PARSED, function() {
                    videoPlayer.play().then(() => {
                        showPlayerStatus('视频播放已开始', 'success');
                    }).catch(error => {
                        showPlayerStatus('播放失败：' + error.message, 'error');
                    });
                });
                
                hlsInstance.on(Hls.Events.ERROR, function(event, data) {
                    console.error('播放错误：', data);
                    let errorMsg = '播放出错，请检查视频源或网络';
                    
                    if (data.fatal) {
                        switch (data.type) {
                            case Hls.ErrorTypes.NETWORK_ERROR:
                                errorMsg = '网络错误：' + (data.details || '无法连接到视频源');
                                break;
                            case Hls.ErrorTypes.MEDIA_ERROR:
                                errorMsg = '媒体错误：' + (data.details || '视频格式不支持');
                                break;
                            default:
                                errorMsg = '未知错误：' + (data.details || '请重试');
                        }
                    }
                    
                    showPlayerStatus(errorMsg, 'error');
                });
            }
            
            function initNativeHlsPlayer(url) {
                videoPlayer.src = url;
                
                videoPlayer.addEventListener('loadedmetadata', function() {
                    videoPlayer.play().then(() => {
                        showPlayerStatus('视频播放已开始', 'success');
                    }).catch(error => {
                        showPlayerStatus('播放失败：' + error.message, 'error');
                    });
                });
                
                videoPlayer.addEventListener('error', function() {
                    showPlayerStatus('播放错误：' + videoPlayer.error.message, 'error');
                });
            }
            
            function showPlayerStatus(message, type) {
                playerStatus.textContent = message;
                playerStatus.className = 'status ' + type;
                playerStatus.style.display = 'block';
                
                if (type !== 'error') {
                    setTimeout(() => {
                        playerStatus.style.display = 'none';
                    }, 3000);
                }
            }
        });
    </script>
</body>
</html>
